<template>
  <!-- 如果是分享隐藏掉返回润物无声的按钮，避免样式过于臃肿 -->
  <!-- <view v-if="props.selected !== 3" class="runwusheng" @click="jumpToOtherMiniPro">
    <md-icon :url="data.configInfo?.xcx_icon" width="102" height="102"></md-icon>
  </view> -->
  <!-- 换成智能问答的入口 -->
  <view v-if="props.selected !== 3">
    <md-go-ai />
  </view>
  <cover-view class="tab-bar">
    <cover-view class="tab-bar-border" />
    <cover-view v-for="(item, index) in data.tabList" :key="index" class="tab-bar-item" @click="switchTab(index)">
      <cover-image :src="item.img" :class="[data.currentIndex === index ? 'active' : 'default']" />
      <cover-view
        :style="{
          color: data.currentIndex === index ? (data.configInfo.select_color_text || data.selectedColor) : data.color,
        }">
        {{ item.name }}
      </cover-view>
    </cover-view>
  </cover-view>
</template>

<script setup lang="ts">
import { reactive } from 'vue';
import { onShow, onLoad } from '@dcloudio/uni-app';
import { bottom_icon, commonConfig } from '@/api/index';

const props = defineProps({
  selected: {
    type: Number,
    default: 0,
  },
});

const data = reactive<any>({
  color: '#7A7E83',
  selectedColor: '#3cc51f',
  currentIndex: 0,
  tabList: [],
  configInfo: {}, // 全局配置信息
});

const jumpToOtherMiniPro = () => {
  // console.log('跳转其他小程序');
  uni.navigateToMiniProgram({
    appId: data.configInfo?.xcx_appid || '',
    path: data.configInfo?.rwws_xcx_page || 'page/index/index',
    // extraData: {
    //   foo: "bar",
    // },
    envVersion: 'release',
    success(res) {
      // 打开成功
      // console.log(res);
    },
  });
};
/**
 * 接口相关
 */

// 底部菜单
const getBottomTab = async () => {
  try {
    const res = await bottom_icon();
    let list = res.data.bottom_icon_list;
    data.tabList = list;
    uni.setStorageSync('tabbar', JSON.stringify(list));
    uni.setStorageSync('myIcons', JSON.stringify(res.data.my_bottom_list));
    uni.setStorageSync('activeDetailIcons', JSON.stringify(res.data.active_detai_list));
    return res.data;
  } catch (err) {}
  return null;
};

// 获取全局配置项
const fetchCommonConfig = async () => {
  try {
    const res = await commonConfig();
    data.configInfo = res.data;
    uni.setStorageSync('configInfo', JSON.stringify(res.data));
  } catch (error) {}
};

defineExpose({
  getBottomTab,
  fetchCommonConfig,
});

onLoad(() => {
  // 跳转润无声按钮处理
  const configInfo = uni.getStorageSync('configInfo');
  if (Object.keys(configInfo || {})?.length > 0) {
    const parseInfo = JSON.parse(configInfo);
    data.configInfo = parseInfo;
  } else {
    fetchCommonConfig();
  }

  // 底部导航处理
  const tabbar = uni.getStorageSync('tabbar');
  if (tabbar) {
    const parseTabbar = JSON.parse(tabbar);
    data.tabList = parseTabbar;
  } else {
    getBottomTab();
  }
});

onShow(() => {
  data.currentIndex = props.selected;
});

const switchTab = (index: number) => {
  uni.switchTab({
    url: `/${data.tabList[index].link}`,
  });
  data.currentIndex = index;
};
</script>

<style lang="scss" scoped>
.tab-bar {
  position: fixed;
  width: 100%;
  bottom: 0;
  left: 0;
  right: 0;
  height: 80px;
  background: white;
  display: flex;
  flex-direction: row;
  padding-bottom: env(safe-area-inset-bottom);
  pointer-events: auto;
  z-index: 999;
}

.tab-bar-border {
  background-color: rgba(0, 0, 0, 0.33);
  position: absolute;
  left: 0;
  top: 0;
  width: 100%;
  height: 1px;
  transform: scaleY(0.5);
}

.tab-bar-item {
  flex: 1;
  text-align: center;
  display: flex;
  justify-content: center;
  align-items: center;
  flex-direction: column;

  .active {
    transform: scale(1.5);
  }
}

.tab-bar-item cover-image {
  width: 70rpx;
  height: 50rpx;
}

.tab-bar-item cover-view {
  font-weight: 500;
  font-size: 24rpx;
  color: #16142c;
  margin-top: 10rpx;
}

.runwusheng {
  position: fixed;
  right: 20rpx;
  top: 60%;
  z-index: 10;
}
</style>
